package com.example.food.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;

import org.json.JSONException;
import org.json.JSONObject;

public class AeliveryActivity extends AppCompatActivity {
    private ImageView img;
    private EditText addressDetail;
    private RadioGroup label;
    private RadioButton ra1;
    private RadioButton ra2;
    private RadioButton ra3;
    private String labelString;
    private RadioButton ra4;
    private EditText name;
    private EditText phone;
    private TextView submit;
    private JSONObject jsonObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aelivery);
        initView();
        savaAddress();
    }

    private void savaAddress() {
        label.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int checkedRadioButtonId = group.getCheckedRadioButtonId();
                if(checkedRadioButtonId==R.id.ra1){
                    labelString = "家";
                }else if(checkedRadioButtonId==R.id.ra2){
                    labelString = "公司";
                }else if(checkedRadioButtonId==R.id.ra3){
                    labelString = "学校";
                }else if(checkedRadioButtonId==R.id.ra4){
                    labelString = "父母家";
                }
            }
        });
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_name = name.getText().toString();
                String str_phone = phone.getText().toString();
                String str_address = addressDetail.getText().toString();

                if (!TextUtils.isEmpty(str_address) && !TextUtils.isEmpty(str_phone) && !TextUtils.isEmpty(str_name)){
                    jsonObject = new JSONObject();
                    try {
                        jsonObject.put("addressDetail",str_address);
                        jsonObject.put("label",labelString);
                        jsonObject.put("name",str_name);
                        jsonObject.put("phone",str_phone);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    savaAddressRequest();
                }else {
                    Toast.makeText(AeliveryActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }

    private void savaAddressRequest() {
        OKHttpTool.doNetRequest("prod-api/api/takeout/address",
                OKHttpTool.POST,
                GeneralClass.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        GeneralClass generalClass = (GeneralClass) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(generalClass.getCode()==200){
                                    Toast.makeText(AeliveryActivity.this, "添加地址成功", Toast.LENGTH_SHORT).show();
                                    // 返回到ProductsettlementActivity并设置返回结果
                                    // 假设你保存了地址信息到result
                                    String result = "New Address Data";

                                    // 设置返回的数据

                                    Intent intent = new Intent();
                                    intent.putExtra("keyName", result);
                                    setResult(RESULT_OK, intent);
                                    AeliveryActivity.this.finish();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                },jsonObject.toString());
    }

    private void initView() {


        img = findViewById(R.id.img);
        addressDetail = findViewById(R.id.addressDetail);
        label = findViewById(R.id.label);
        ra1 = findViewById(R.id.ra1);
        ra2 = findViewById(R.id.ra2);
        ra3 = findViewById(R.id.ra3);
        ra4 = findViewById(R.id.ra4);
        name = findViewById(R.id.name);
        phone = findViewById(R.id.phone);
        submit = findViewById(R.id.submit);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AeliveryActivity.this.finish();
            }
        });
    }
}